﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Falcon.Core.Extensions;

namespace Falcon.Core.Helpers
{
	using System.Web.Mvc;
	using System.Web.Routing;
	using Falcon.Core.Domain;

	public class RouteHelper
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="RouteHelper"/> class.
		/// </summary>
		/// <param name="value">The value.</param>
		public RouteHelper(string value)
		{
			this.Value = value;
		}

		/// <summary>
		/// Gets or sets the value.
		/// </summary>
		/// <value>The value.</value>
		public string Value
		{
			get;
			private set;
		}

		/// <summary>
		/// Links the specified text.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <returns></returns>
		public string Link(string text)
		{
			return Link(text, null);
		}

		/// <summary>
		/// Links the specified text.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="attributes">The attributes.</param>
		/// <returns></returns>
		public string Link(string text, object attributes)
		{
			var link = new TagBuilder("a");

			link.MergeAttribute("href", this.Value);

			if (attributes != null)
			{
				link.MergeAttributes(new RouteValueDictionary(attributes));
			}

			link.SetInnerText(text);

			return link.ToString(TagRenderMode.Normal);
		}

		/// <summary>
		/// Displays the image from the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <returns></returns>
		public string Image(string source)
		{
			return Image(source, true, null);
		}

		/// <summary>
		/// Displays the image from the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <param name="attributes">The attributes.</param>
		/// <returns></returns>
		public string Image(string source, object attributes)
		{
			return Image(source, true, attributes);
		}

		/// <summary>
		/// Displays the image from the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <param name="root">if set to <c>true</c> [root].</param>
		/// <param name="attributes">The attributes.</param>
		/// <returns></returns>
		public string Image(string source, bool root, object attributes)
		{
			var link = new TagBuilder("a");

			link.MergeAttribute("href", this.Value);

			var image = new TagBuilder("img");

			image.MergeAttribute("src", (root) ? string.Concat(Settings.ImagePath, "/", source).AsRoot() : source);

			if (attributes != null)
			{
				image.MergeAttributes(new RouteValueDictionary(attributes));
			}

			link.InnerHtml = image.ToString(TagRenderMode.SelfClosing);

			return link.ToString(TagRenderMode.Normal);
		}
	}
}